﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using Microsoft.SolverFoundation.Services;
using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Solvers;



namespace program_new5
{
    public class Program
    {
                static void Main(string[] args)
                 {
              //  //创建Solver内容
              //  SolverContext solver = SolverContext.GetContext();
              //  Microsoft.SolverFoundation.Services.Model model = solver.CreateModel();

              //  Domain Pgmax=Domain.;
                
              // /float [] min = new float[arryPgmin.Length];
              //for (int m=0;m<arryPgmin.Length;m++)
              // // {
              //   min[m] // dicision不会定义成数组类型
              // // }
                    // 1. 打开excel文件，读入原始数据
                    String strFileName = "testData/case30.xlsx";
                    OpenExcel(strFileName);
                    // 2. 生成solver foundation模型对象
                    createOptimModel();
                    // 3. 求解solver foundation模型，输出结果
                    // 如果求解过程比较简单，也可以不单独建立方法，直接把代码写在这里


              }

                // 这里写利用读到的原始数据建立优化模型对象的代码
                private static void createOptimModel()
                {
                    throw new NotImplementedException();
                }

        // 读入原始数据
        // 这里返回类型为void，因此要想好读到的数怎么才能被其他函数或方法用到，可以考虑建立类的属性来存储数据
        private static void OpenExcel(string strFileName)
        {
            object missing = System.Reflection.Missing.Value;
            Excel._Application ExcelApp = new Excel.Application();//创建一个Excel实例

            if (ExcelApp == null)
            {
                Console.WriteLine("<script>alert('Can't access excel')</script>");
            }
            else
            {

                // 从磁盘指定位置打开一个 Excel 文件
                Workbook wb = ExcelApp.Workbooks.Open(@"testData\case30.xlsx", Missing.Value, Missing.Value,
         Missing.Value, Missing.Value, Missing.Value, Missing.Value,
         Missing.Value, Missing.Value, Missing.Value, Missing.Value,
         Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                //得到第一个工作簿(sheet2、4）
                Worksheet ws1 = (Excel.Worksheet)wb.Worksheets["Sheet2"];
                Worksheet ws2 = (Excel.Worksheet)wb.Worksheets["Sheet4"];


                ExcelApp.Visible = true;// 显示 Excel 文件


                //取得总记录行数   (包括标题列)
                int rowsint1 = ws1.UsedRange.Cells.Rows.Count; //得到行数
                int columnsint1 = ws1.UsedRange.Cells.Columns.Count;//得到列数

                int rowsint2 = ws2.UsedRange.Cells.Rows.Count; //得到行数
                int columnsint2 = ws2.UsedRange.Cells.Columns.Count;//得到列数

                //取得数据范围区域 (不包括标题列) 
                Range rng1 = ws1.Cells.get_Range("I1", "I" + rowsint1);   //最大有功功率输出（MW）
                Range rng2 = ws1.Cells.get_Range("J1", "J" + rowsint1); //最小有功功率输出（MW）

                Range rng3 = ws2.Cells.get_Range("E1", "E" + rowsint2);   //最大有功功率输出（MW）
                Range rng4 = ws2.Cells.get_Range("F1", "F" + rowsint2); //最小有功功率输出（MW）
                Range rng5 = ws2.Cells.get_Range("G1", "G" + rowsint2);   //最大有功功率输出（MW）


                object[,] arryPgmax = (object[,])rng1.Value2;   //get range's value
                object[,] arryPgmin = (object[,])rng2.Value2;
                object[,] arryC2 = (object[,])rng3.Value2;
                object[,] arryC1 = (object[,])rng4.Value2;
                object[,] arryC0 = (object[,])rng5.Value2;


                ExcelApp.Quit(); ExcelApp = null;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
 
                
        }
    }
}
